﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Helpers;
using Warehouse_Management.New;

namespace Warehouse_Management
{
    public partial class mainForm : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public mainForm()
        {
            InitializeComponent();
        }

        private void mainForm_Load(object sender, EventArgs e)
        {
            try
            {
                Global.LoadDVT();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Lỗi kết nối hệ thống. Vui lòng cấu hình thông tin database.\r\n " + ex.Message);
                frmCauHinh f = new frmCauHinh();
                f.ShowDialog(this);
            }
            setStatus(0);
            DevExpress.XtraBars.Helpers.SkinHelper.InitSkinGallery(ribbonGalleryBarItem1, true, true);
             this.btnSave.Enabled = this.BtnNew.Enabled = this.btnImportExcel.Enabled = this.btnEdit.Enabled = false;
            KhoUC_Load();
        }
        /// <summary>
        /// set color of ribbon page 
        /// </summary>
        /// <param name="status">
        /// status = 1 (import) - 2 (export) - 3(Catalog) - 4(warehose) - 5(liquidity) - 6(Measure)
        /// </param>
        private void setStatus(TrangThaiChungTu TrangThaiChungTu)
        {
            btnSave.Enabled = false;
            switch (TrangThaiChungTu)
            {
                case TrangThaiChungTu.PhieuNhap:
                    rboPageMain.Color = Color.Green;
                     this.btnSave.Enabled = true;
                    this.BtnNew.Enabled = this.btnImportExcel.Enabled = this.btnEdit.Enabled = true;
                    rboPageMain.Text = "NHẬP HÀNG (IMPORT)";
                    break;
                case TrangThaiChungTu.PhieuXuat:
                    rboPageMain.Color = Color.Red;
                     this.btnSave.Enabled = true;
                    this.BtnNew.Enabled = this.btnImportExcel.Enabled = this.btnEdit.Enabled = true;
                    rboPageMain.Text = "XUẤT HÀNG (EXPORT)";
                    break;
                case TrangThaiChungTu.Hang:
                    rboPageMain.Color = Color.Gray;
                     this.btnSave.Enabled = true;
                    this.BtnNew.Enabled = this.btnImportExcel.Enabled = this.btnEdit.Enabled = true;
                    rboPageMain.Text = "DANH MỤC HÀNG HÓA";
                    break;
                case TrangThaiChungTu.Kho:
                    rboPageMain.Color = Color.Gray;
                    this.btnImportExcel.Enabled = false;
                    this.BtnNew.Enabled = this.btnSave.Enabled = this.btnEdit.Enabled = true;
                    rboPageMain.Text = "DANH MỤC NHÀ SÁCH";
                    break;
                case TrangThaiChungTu.ThanhKhoan:
                    rboPageMain.Color = Color.Blue;
                    rboPageMain.Text = "THANH KHOẢN";
                    break;
                case TrangThaiChungTu.DonViTinh:
                    rboPageMain.Color = Color.Yellow;
                    this.btnSave.Enabled = true;
                    this.BtnNew.Enabled = this.btnImportExcel.Enabled = this.btnEdit.Enabled = false;
                    rboPageMain.Text = "DANH MỤC ĐƠN VỊ TÍNH";
                    break;
                default :
                    rboPageMain.Color = Color.Silver;
                    rboPageMain.Text = "CHƯƠNG TRÌNH QUẢN LÝ KHO HÀNG HÓA CÔNG TY THIÊN LONG";
                    break;

            }

        }

        private void barXuat_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            setStatus(TrangThaiChungTu.PhieuXuat);
            Form[] forms = this.MdiChildren;
            for (int i = 0; i < forms.Length; i++)
            {
                if ((TrangThaiChungTu)forms[i].Tag == TrangThaiChungTu.PhieuXuat)
                {
                    forms[i].Activate();
                    return;
                }
            }
            frmXuatManager f = new frmXuatManager();
            f.MdiParent = this;
            f.Tag = TrangThaiChungTu.PhieuXuat;
            f.Show();
        }

        private void barNhap_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            setStatus(TrangThaiChungTu.PhieuNhap);
            Form[] forms = this.MdiChildren;
            for (int i = 0; i < forms.Length; i++)
            {
                if ((TrangThaiChungTu)forms[i].Tag == TrangThaiChungTu.PhieuNhap )
                {
                    forms[i].Activate();
                    return;
                }
            }
            frmXuatManager f = new frmXuatManager();
            f.MdiParent = this;
            f.Tag = TrangThaiChungTu.PhieuNhap;
            f.Show();
            

        }

        private void barKho_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            setStatus(TrangThaiChungTu.Kho);
            Form[] forms = this.MdiChildren;
            for (int i = 0; i < forms.Length; i++)
            {
                if (forms[i].Name.ToString().Equals("frmDanhMucKho"))
                {
                    forms[i].Activate();
                    return;
                }
            }
            frmDanhMucKho f = new frmDanhMucKho();
            f.MdiParent = this;
            f.Tag = TrangThaiChungTu.Kho;
            f.Show();
        }

        private void barHang_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            setStatus(TrangThaiChungTu.Hang);
            Form[] forms = this.MdiChildren;
            for (int i = 0; i < forms.Length; i++)
            {
                if (forms[i].Name.ToString().Equals("frmDanhMucHangHoa"))
                {
                    forms[i].Activate();
                    return;
                }
            }
            frmDanhMucHangHoa f = new frmDanhMucHangHoa();
            f.MdiParent = this;
            f.Tag = TrangThaiChungTu.Hang;
            f.Show();
        }

        private void barThanhKhoan_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            setStatus(TrangThaiChungTu.ThanhKhoan);
            Form[] forms = this.MdiChildren;
            for (int i = 0; i < forms.Length; i++)
            {
                if (forms[i].Name.ToString().Equals("frmDanhSachTK"))
                {
                    forms[i].Activate();
                    return;
                }
            }
            frmDanhSachTK f = new frmDanhSachTK();
            f.MdiParent = this;
            f.Tag = TrangThaiChungTu.ThanhKhoan;
            f.Show();
        }

        private void mainForm_StyleChanged(object sender, EventArgs e)
        {

        }

        private void ribbonGalleryBarItem1_GalleryItemClick(object sender, DevExpress.XtraBars.Ribbon.GalleryItemClickEventArgs e)
        {
           // Global.SaveNodeXmlAppSettings("Skin", e.Item.Caption);
            Global.SaveConfig("Skin", e.Item.Caption);
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = e.Item.Caption.ToString();
        }
        private TrangThaiChungTu formActive()
        {
            Form f = this.ActiveMdiChild;
            return (TrangThaiChungTu)f.Tag;
        }
        #region  button New
        
     
        private void BtnNew_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                switch (formActive())
                {
                    case TrangThaiChungTu.PhieuNhap:
                        frmXuatManager fN = (frmXuatManager)this.ActiveMdiChild;
                        fN.TaoMoi();
                        break;
                    case TrangThaiChungTu.PhieuXuat:
                        frmXuatManager fX = (frmXuatManager)this.ActiveMdiChild;
                        fX.TaoMoi();
                        break;
                    case TrangThaiChungTu.Hang:
                        frmDanhMucHangHoa fH = (frmDanhMucHangHoa)this.ActiveMdiChild;
                        fH.NewHang();
                        break;
                    case TrangThaiChungTu.Kho:
                        frmDanhMucKho fK = (frmDanhMucKho)this.ActiveMdiChild;
                        fK.Tag = TrangThaiChungTu.Kho;
                        fK.NewHang();
                        break;
                    case TrangThaiChungTu.DonViTinh:
                        frmDanhMucDVT fD = (frmDanhMucDVT)this.ActiveMdiChild;
                        //fD.TaoMoi();
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
   #endregion

        private void xtraTabbedMdiManager1_SelectedPageChanged(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild != null)
                setStatus((TrangThaiChungTu)this.ActiveMdiChild.Tag);
        }
        #region seach

        
        private void seach()
        {
            string condition = string.Empty;
            if (!string.IsNullOrEmpty(lueTenKho.EditValue.ToString()))
                condition += "AND MaKho = '" + lueTenKho.EditValue.ToString() + "'";
            if (dateDenNgay.EditValue != null)
                condition += "AND NgayNhap >= '" + ((DateTime)dateTuNgay.EditValue).ToString("yyyy-MM-dd") + "'";
            if (dateDenNgay.EditValue != null)
                condition += "AND NgayNhap <= '" + ((DateTime)dateDenNgay.EditValue).ToString("yyyy-MM-dd") + "'";
            if (!string.IsNullOrEmpty(condition))
            {
                switch (formActive())
                {
                    case TrangThaiChungTu.PhieuNhap:
                        frmXuatManager fN = (frmXuatManager)this.ActiveMdiChild;
                        fN.seach(condition);
                        break;
                    case TrangThaiChungTu.PhieuXuat:
                        frmXuatManager fX = (frmXuatManager)this.ActiveMdiChild;
                        fX.seach(condition);
                        break;
//                     case TrangThaiChungTu.Hang:
//                         frmHang fH = new frmHang();
//                         fH.Tag = TrangThaiChungTu.Hang;
//                         fH.ShowDialog();
//                         break;
//                     case TrangThaiChungTu.Kho:
//                         frmKho fK = new frmKho();
//                         fK.Tag = TrangThaiChungTu.Kho;
//                         fK.ShowDialog();
//                         break;
                }
            }
        }
       

        private void btnSeach_ItemClick(object sender, ItemClickEventArgs e)
        {
            seach();
        }
 #endregion  

        private void KhoUC_Load()
        {
            IList<KhoHang> DMKho = KhoHang.SelectCollectionAll();
            repositoryItemLookUpEdit1.DataSource = DMKho;
            repositoryItemLookUpEdit1.ValueMember = "MaKho";
            repositoryItemLookUpEdit1.DisplayMember = "TenKho";

        }

        private void barDonViTinh_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            setStatus(TrangThaiChungTu.DonViTinh);
            Form[] forms = this.MdiChildren;
            for (int i = 0; i < forms.Length; i++)
            {
                if (forms[i].Name.ToString().Equals("frmDanhMucDVT"))
                {
                    forms[i].Activate();
                    return;
                }
            }
            frmDanhMucDVT f = new frmDanhMucDVT();
            f.MdiParent = this;
            f.Tag = TrangThaiChungTu.DonViTinh;
            f.Show();
        }

        private void btnSave_ItemClick(object sender, ItemClickEventArgs e)
        {
            switch (formActive())
            {
                case TrangThaiChungTu.DonViTinh:
                    frmDanhMucDVT fD = (frmDanhMucDVT)this.ActiveMdiChild;
                    fD.Save();
                    break;
                case TrangThaiChungTu.Hang:
                    frmDanhMucHangHoa fH = (frmDanhMucHangHoa)this.ActiveMdiChild;
                    fH.Save();
                    break;
                case TrangThaiChungTu.Kho:
                    frmDanhMucKho fK = (frmDanhMucKho)this.ActiveMdiChild;
                    fK.Save();
                    break;
            }
        }

        private void btnImportExcel_ItemClick(object sender, ItemClickEventArgs e)
        {
            switch (formActive())
            {
                case TrangThaiChungTu.Hang:
                    frmDanhMucHangHoa fH = (frmDanhMucHangHoa)this.ActiveMdiChild;
                    fH.import();
                    break;
                
            }
        }

        private void btnDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                switch (formActive())
                {
                    case TrangThaiChungTu.PhieuNhap:
                        frmXuatManager fN = (frmXuatManager)this.ActiveMdiChild;
                        fN.delete();
                        break;
                    case TrangThaiChungTu.PhieuXuat:
                        frmXuatManager fX = (frmXuatManager)this.ActiveMdiChild;
                        fX.delete();
                        break;
                    case TrangThaiChungTu.Hang:
                        frmDanhMucHangHoa fH = (frmDanhMucHangHoa)this.ActiveMdiChild;
                        fH.DeleteHang();
                        break;
                    case TrangThaiChungTu.Kho:
                        frmDanhMucKho fK = (frmDanhMucKho)this.ActiveMdiChild;
                        fK.Tag = TrangThaiChungTu.Kho;
                        fK.Delete();
                        break;
                    case TrangThaiChungTu.DonViTinh:
                        frmDanhMucDVT fD = (frmDanhMucDVT)this.ActiveMdiChild;
                        fD.delete();
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnEdit_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                switch (formActive())
                {
                    case TrangThaiChungTu.PhieuNhap:
                        frmXuatManager fN = (frmXuatManager)this.ActiveMdiChild;
                        fN.EditPhieu();
                        break;
                    case TrangThaiChungTu.PhieuXuat:
                        frmXuatManager fX = (frmXuatManager)this.ActiveMdiChild;
                        fX.EditPhieu();
                        break;
                    case TrangThaiChungTu.Hang:
                        frmDanhMucHangHoa fH = (frmDanhMucHangHoa)this.ActiveMdiChild;
                        fH.EditHang();
                        break;
                    case TrangThaiChungTu.Kho:
                        frmDanhMucKho fK = (frmDanhMucKho)this.ActiveMdiChild;
                        fK.Tag = TrangThaiChungTu.Kho;
                        fK.EditKho();
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCauHinh_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmCauHinh f = new frmCauHinh();
            f.ShowDialog(this);
        }

       
    }
}